﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tab2Sql
{
    /// <summary>
    /// Global class. Unused as of November 2008.
    /// </summary>
    public static class Global
    {
        /// <summary>
        /// global value for generated sql
        /// </summary>
        public static string generatedSQL = "";
        /// <summary>
        /// global variable for input
        /// </summary>
        public static string inputBunk = "";
        
    }

    /// <summary>
    /// Pop up form for interacting directly with db via SQL connection.
    /// </summary>
    public partial class DataConfig : Form
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        public DataConfig()
        {
            InitializeComponent();
            sqlStatement.Text = Global.generatedSQL;
            sqlStatement.Focus();

            
        }
        
        private void DataConfig_Load(object sender, EventArgs e)
        {
            sqlStatement.Focus();
        }

        private void DataConfig_Load_1(object sender, EventArgs e)
        {




            sqlStatement.Focus();

            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            statusBox.Clear();
            int Rows = 0;
            if (sqlStatement.Text.ToLower().StartsWith("select"))
            {
                try
                {
                    statusBox.Text += DT2Text(RenderSQL(sqlStatement.Text));
                }
                catch (Exception ex)
                {
                    statusBox.Text += ex.Message;
                }
            }
            else
            {
                try
                {
                    Rows = DoSQL(sqlStatement.Text);
                    statusBox.Text += Rows.ToString() + " row(s) affected";
                }
                catch (Exception ex)
                {
                    statusBox.Text += ex.Message;
                }
            }
        }


        private int ExecuteSQL(SqlCommand cmdX)
        {
            int rowCount = 0;
            using (SqlConnection conn = new SqlConnection(connStringBox.Text))
            {
                conn.Open();
                cmdX.Connection = conn;
                try
                { rowCount = cmdX.ExecuteNonQuery(); }
                catch (Exception ex)
                {
                    statusBox.Text += ex.Message.ToString();
                    rowCount = -1;
                }
                return rowCount;
            }
        }
        /// <summary>
        /// In SQL statement, Out : Row Count only. Runs via ExecuteNonQuery - no results!
        /// </summary>
        /// <param name="sqlString"></param>
        /// <returns></returns>
        public int DoSQL(string sqlString)
        {
            int rowCount = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sqlString;
            rowCount = ExecuteSQL(cmd);
            return rowCount;

        }

        private string DT2Text(DataTable dt)
        {
            System.Text.StringBuilder endS = new System.Text.StringBuilder();
            if (dt.Rows.Count > 2000)
            {
                return "More than 2000 Rows Returned. Cancelled. This is not a query toool.";
            }
            else
            {
                foreach (DataRow r in dt.Rows)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        try
                        {
                            endS.Append(r.ItemArray.GetValue(i).ToString());
                            endS.Append("\t");
                        }
                        catch (Exception ex)
                        {

                            MessageBox.Show(ex.Message);

                            /* whateva. */
                        }
                    }
                    endS.AppendLine();
                }
                return endS.ToString();
            }
        }


        private DataTable RenderSQL(string SqlString)
        {
            // connection related
            DataTable dt = new DataTable();

            using (SqlConnection conn = new SqlConnection(connStringBox.Text))
            {
                using (SqlCommand cmd = new SqlCommand(SqlString, conn))
                {
                    conn.Open();
                    try
                    {

                        IDataReader dr = cmd.ExecuteReader();
                        if (dr.RecordsAffected > 2000)
                        {
                            MessageBox.Show("Too many rows.");
                            return dt;
                        }
                        else
                        {
                            dt.Load(dr);
                        }

                        return dt;
                    }
                    catch (Exception ex)
                    {
                        statusBox.Text += ex.Message;

                    }
                }


                return dt;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(statusBox.Text);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            sqlStatement.Clear();
            statusBox.Clear();
            sqlStatement.Focus();
        }

    }

    class SqlHelper
    {
        public static bool isSQLSafeToRun(string sqlStatement)
        {
            string input = sqlStatement;
            
            return false;
        }
    
    }
    




}
